package liming.tool.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FindUser {
    static class UserInfo {
        String name;
        String roomNumber;
        boolean hasNotice;
        boolean hasReceipt;
        boolean hasLoan;

        public UserInfo(String name, String roomNumber) {
            this.name = name;
            this.roomNumber = roomNumber;
        }

        public boolean isHasLoan() {
            return hasLoan;
        }

        public boolean isHasNotice() {
            return hasNotice;
        }

        public boolean isHasReceipt() {
            return hasReceipt;
        }

        @Override
        public String toString() {
            return String.format("| %-6s | %-8s | %-7s | %-4s | %-8s |%n",
                    name,
                    roomNumber,
                    hasNotice ? "✅" : "❌",
                    hasReceipt ? "✅" : "❌",
                    hasLoan ? "✅" : "❌");
        }
    }

    public static void main(String[] args) throws IOException {
        String root  = "C:\\Users\\Administrator\\Desktop\\监管户";
        File rootDir = new File(root); // 替换为实际路径
        List<UserInfo> userList = new ArrayList<>();

        // 遍历主目录下的所有子文件夹
        for (File subDir : rootDir.listFiles(File::isDirectory)) {
            // 解析姓名和房号
            String[] nameAndRoom = parseFolderName(subDir.getName());
            if (nameAndRoom == null) continue;

            UserInfo user = new UserInfo(nameAndRoom[0], nameAndRoom[1]);

            // 检查文件存在情况
            for (File file : subDir.listFiles(File::isFile)) {
                String baseName = getBaseName(file.getName());
                checkFileStatus(user, baseName);
            }

            userList.add(user);
        }

        // 输出结果
        printResults(userList);
    }

    private static String[] parseFolderName(String folderName) {
        Pattern pattern = Pattern.compile("^(\\D+)(\\d.+)$");
        Matcher matcher = pattern.matcher(folderName);
        if (matcher.find()) {
            return new String[]{matcher.group(1), matcher.group(2)};
        }
        return null;
    }

    private static String getBaseName(String fileName) {
        int dotIndex = fileName.lastIndexOf('.');
        return dotIndex == -1 ? fileName : fileName.substring(0, dotIndex);
    }

    private static void checkFileStatus(UserInfo user, String baseName) {
        if (baseName.startsWith(user.name + "通知书")) {
            user.hasNotice = true;
        } else if (baseName.startsWith(user.name + "回单")) {
            user.hasReceipt = true;
        } else if (baseName.indexOf("放款")>0) {
            user.hasLoan = true;
        }
    }

    private static void printResults(List<UserInfo> userList) throws IOException {
        StringBuilder sb = new StringBuilder();
        sb.append("| 姓名   | 房号     | 通知书 | 回单 | 按揭放款 |\n");
        sb.append("|--------|----------|---------|------|----------|\n");

        StringBuilder sb1 = new StringBuilder("无回单\n\n").append(sb);
        StringBuilder sb2 = new StringBuilder("无通知书\n\n").append(sb);
        StringBuilder sb3 = new StringBuilder("无放款\n\n").append(sb);

        for (UserInfo user : userList) {
            sb.append(user);
            if (!user.isHasReceipt())
                sb1.append(user);
            if (!user.isHasNotice())
                sb2.append(user);
            if (!user.isHasLoan())
                sb3.append(user);
        }
        System.out.println(sb);
        File root = new File("./files/room");
        FileRW.writeTextFile(new File(root,  "总计.md"), sb.toString(), false, FileRW.FileEncoding.UTF8);
        FileRW.writeTextFile(new File(root,  "缺回单.md"), sb1.toString(), false, FileRW.FileEncoding.UTF8);
        FileRW.writeTextFile(new File(root,  "无通知书.md"), sb2.toString(), false, FileRW.FileEncoding.UTF8);
        FileRW.writeTextFile(new File(root,  "无放款.md"), sb3.toString(), false, FileRW.FileEncoding.UTF8);
    }
}
